import pygame
from pygame.locals import *
from sys import exit
import random
import time
import tkinter
from tkinter import *
from tkinter import messagebox

class blink:
    def __init__(self,x,y,screen):
        self.x=x
        self.y=y
        self.screen=screen
        self.image=None
        self.n=0

    def display(self):
        self.image=pygame.image.load('blink'+str(self.n)+'.png')
        self.screen.blit(self.image,(self.x,self.y))

class garbage0:

    def __init__(self,screen):
        self.image=pygame.image.load('garbage0.png')
        self.y=-119
        self.x=random.randrange(1119)
        self.screen=screen

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,speed):
        self.y+=speed
        if self.y>920:
            return True

    def check(self,player):
        if self.x>player.x-2 and self.x<player.x+141 and self.y>player.y-118 and self.y<player.y+215:
            return True

class garbage1:

    def __init__(self,screen):
        self.image=pygame.image.load('garbage1.png')
        self.screen=screen
        self.x=random.randrange(1181)
        self.y=-122

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,speed):
        self.y +=speed
        if self.y > 920:
            return True

    def check(self,player):
        if self.x>player.x-2 and self.x<player.x+203 and self.y>player.y-121 and self.y<player.y+215:
            return True

class garbage2:

    def __init__(self,screen):
        self.image=pygame.image.load('garbage2.png')
        self.screen=screen
        self.x=random.randrange(1181)
        self.y=-100

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,speed):
        self.y +=speed
        if self.y > 920:
            return True

    def check(self,player):
        if self.x> player.x-2 and self.x < player.x + 128 and self.y > player.y-99 and self.y < player.y + 215:
            return True

class garbage3:

    def __init__(self,screen):
        self.image=pygame.image.load('garbage3.png')
        self.screen=screen
        self.x=random.randrange(1106)
        self.y=-89

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,speed):
        self.y +=speed
        if self.y > 920:
            return True

    def check(self,player):
        if self.x> player.x-2 and self.x < player.x + 128 and self.y > player.y-88 and self.y < player.y + 215:
            return True

class garbage4:

    def __init__(self,screen):
        self.image=pygame.image.load('garbage4.png')
        self.screen=screen
        self.x=random.randrange(1106)
        self.y=-89

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self,speed):
        self.y +=speed
        if self.y > 920:
            return True

    def check(self,player):
        if self.x> player.x-2 and self.x < player.x + 130  and self.y > player.y-118 and self.y < player.y + 215:
            return True

class player_net:

    def __init__(self,screen):
        self.screen=screen
        self.x=500
        self.y=520
        self.image=pygame.image.load('net.png')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move_right(self):
        self.x+=10
        if self.x>1000:
            self.x=1000

    def move_left(self):
        self.x-=10
        if self.x<0:
            self.x=0

    def move_up(self):
        self.y-=10
        if self.y<0:
            self.y=0

    def move_down(self):
        self.y+=10
        if self.y>585:
            self.y=585

def keyboard_control(player):

    keypress=pygame.key.get_pressed()

    if keypress[K_LEFT]:
        player.move_left()

    elif keypress[K_RIGHT]:
        player.move_right()

    if keypress[K_UP]:
        player.move_up()

    elif keypress[K_DOWN]:
        player.move_down()

def getinput():
    def return_callback(event):
        root.quit()
    def close_callback():
        exit()
    root = Tk(className='Python 海洋垃圾打捞小游戏')
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 450
    height = 120
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
    root.geometry(size)
    root.resizable(0, 0)
    lable1=Label(root,height=2)
    lable1['text']='欢迎来到海洋垃圾打捞小游戏,每捕获20个垃圾游戏进入下一关，\n每过一关垃圾掉落的速度加快。每错过一个垃圾生命值减1，生命值为0时游戏结束。'
    lable1.pack()
    lable2 = Label(root, height=2)
    lable2['text'] = '请输入你的生命值，请输入0-10内的整数，按Enter结束。'
    lable2.pack()
    entry = Entry(root)
    entry.bind('<Return>', return_callback)
    entry.pack()
    entry.focus_set()
    root.protocol("WM_DELETE_WINDOW", close_callback)
    root.mainloop()
    str = entry.get()
    root.destroy()
    return str

def getreinput():
    def return_callback(event):
        root.quit()
    def close_callback():
        exit()
    root = Tk(className='Python 海洋垃圾打捞小游戏')
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 400
    height = 120
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
    root.geometry(size)
    root.resizable(0, 0)
    lable1=Label(root,height=2)
    lable1['text']='欢迎来到海洋垃圾打捞小游戏'
    lable1.pack()
    lable2 = Label(root, height=2)
    lable2['text'] = '请输入你的生命值，请输入0-10内的整数，按Enter结束。'
    lable2.pack()
    entry = Entry(root)
    entry.bind('<Return>', return_callback)
    entry.pack()
    entry.focus_set()
    lable3 = Label(root, height=4, fg='red', font=('黑体', 14, 'bold'))
    lable3['text'] = '无效的输入，请输入0-10内的整数。'
    lable3.pack()
    root.protocol("WM_DELETE_WINDOW", close_callback)
    root.mainloop()
    str = entry.get()
    root.destroy()
    return str

def main():
    life=0
    try:
        life=int(getinput())
        if not 0<life<=10:
            raise ValueError
    except Exception:
        while not 0<life<=10:
            try:
                life=int(getreinput())
            except Exception:
                pass

    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load('background.mp3')
    pygame.mixer.music.play(-1,0.0)
    sound=pygame.mixer.Sound('win.wav')
    window=pygame.display.set_mode((1280,800),0,32)
    backgound=pygame.image.load('sea.png')
    player=player_net(window)
    pygame.display.set_caption('Python 海洋垃圾打捞小游戏')
    garbage_list=[]
    blink_list=[]
    speed=3
    score=0
    n=0

    while True:
        if score // 20 > n:
            n += 1
            speed += 2

        window.blit(backgound,(0,0))
        player.display()
        keyboard_control(player)
        random_number=random.randrange(150)

        if random_number==0:
            garbage_list.append(garbage0(window))

        if random_number==1:
            garbage_list.append(garbage1(window))

        if random_number==2:
            garbage_list.append(garbage2(window))

        if random_number==3:
            garbage_list.append(garbage3(window))

        if random_number==4:
            garbage_list.append(garbage4(window))

        for garbage in garbage_list:
            garbage.display()
            if garbage.move(speed):
                garbage_list.remove(garbage)
                life-=1

            if garbage.check(player):
                garbage_list.remove(garbage)
                score+=1
                sound.play(0)
                blink_list.append(blink(garbage.x,garbage.y,window))
                print('你当前的分数为：',score)

        for b in blink_list:
            b.display()
            b.n+=1
            if b.n==5:
                blink_list.remove(b)

        if life==0:
            print('你的命已经用完了！')
            break

        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
        pygame.display.update()

        time.sleep(0.02)

if __name__=='__main__':
    main()